package com.fire.social.mapper;

import com.fire.social.entity.node.User;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper extends Neo4jRepository<User, Long> {

    /**
     * 查询所有用户
     */
    @Query("match (user:User) return user")
    List<User> getAllUser();

    /**
     * 登录
     */
    @Query("MATCH (user:User) WHERE user.account= $account AND user.password= $password RETURN user")
    User login(@Param("account") String account, @Param("password") String password);

    /**
     * 根据账号获取用户信息
     */
    @Query("MATCH (user:User) WHERE user.account=$account RETURN user")
    User getUserByAccount(@Param("account") String account);

    /**
     * 删除用户，同时删除关联的关系
     */
    @Query("match (user:User)-[r]-() where user.account=$account delete user,r return user")
    User deleteUserByAccount(@Param("account") String account);

    /**
     * 用户新增
     */
    @Query("CREATE (user:User{account:$account,password:$password," +
            "nickname:$nickname,age:$age,gender:$gender," +
            "email:$email, imgurl:$imgurl}) RETURN user")
    User createUser(@Param("account") String account,@Param("password") String password,
                 @Param("nickname") String nickname,@Param("age") Integer age,
                 @Param("gender") String gender,@Param("email") String email, @Param("imgurl") String imgurl);


    /**
     * 我关注的人
     */
    @Query("match (me:User) where me.account=$account\n" +
            "match (me)-[:Follow]->(users) return users ")
    List<User> getMyFollow(@Param("account")String account);
}
